<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: AggregatorListener.cfc 1003 2006-12-20 20:30:23Z mpwoodward $

Notes:
This listener is autowired by ColdSpring.
--->
<cfcomponent
	displayname="AggregatorListener"
	extends="MachII.framework.Listener"
	output="false"
	hint="AggregatorListener for MachBlog.">

	<!---
	INITIALIZATION / CONFIGURATION
	--->
	<cffunction name="configure" access="public" output="false" returntype="void" 
		hint="Configures this listener as part of the Mach-II framework.">
		<!--- Does nothing. --->
	</cffunction>

	<cffunction name="setAggregatorService" access="public" output="false" returntype="void"
		hint="Dependency: injected">
		<cfargument name="aggregatorService" type="org.machblog.aggregator.AggregatorService" required="true" />
		<cfset variables.aggregatorService = arguments.aggregatorService />
	</cffunction>	
	<cffunction name="getAggregatorService" access="private" output="false" returntype="org.machblog.aggregator.AggregatorService">
		<cfreturn variables.aggregatorService />
	</cffunction>

	<cffunction name="setUserFacade" access="public" output="false" returntype="void"
		hint="Dependency: injected">
		<cfargument name="userFacade" type="org.machblog.facades.UserFacade" required="true" />
		<cfset variables.userFacade = arguments.userFacade />
	</cffunction>	
	<cffunction name="getUserFacade" access="private" output="false" returntype="org.machblog.facades.UserFacade">
		<cfreturn variables.userFacade />
	</cffunction>

	<!---
	PUBLIC FUNCTIONS - GATEWAY RELATED
	--->
	<cffunction name="getAllAggregators" access="public" output="false" returntype="query" 
			hint="Returns a query object containing all aggregators (active and inactive)">
		<cfreturn getAggregatorService().getAllAggregators() />
	</cffunction>
	
	<cffunction name="getActiveAggregators" access="public" output="false" returntype="query" 
			hint="Returns a query object containing active aggregators">
		<cfreturn getAggregatorService().getActiveAggregators() />
	</cffunction>
	
	<cffunction name="getAutoPingAggregators" access="public" output="false" returntype="query" 
			hint="Returns a query object containing aggregators set to be auto-pinged">
		<cfreturn getAggregatorService().getAutoPingAggregators() />
	</cffunction>
	
	<cffunction name="pingAggregator" access="public" output="false" returntype="void" hint="Pings a single aggregator">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var exitEvent = arguments.event.getArg("exitEvent", "showAggregatorAdmin_redirect") />
		<cfset var message = getProperty("resourceBundleService").getResourceBundle().getResource("aggregatorpingsuccessmessage") />
		
		<cftry>
			<cfset getAggregatorService().pingAggregator(arguments.event.getArg("aggregatorId")) />
			<cfcatch type="any">
				<cfset message = getProperty("resourceBundleService").getResourceBundle().getResource("aggregatorpingfailedmessage") />
			</cfcatch>
		</cftry>
		
		<cfset arguments.event.setArg("message", message) />
		<cfset announceEvent(exitEvent, arguments.event.getArgs()) />
	</cffunction>
	
	<!---
	PUBLIC FUNCTIONS - DAO RELATED
	--->
	<cffunction name="getAggregator" access="public" output="false" returntype="org.machblog.aggregator.Aggregator"
		hint="Retrieves an aggregator by ID">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var aggregator = 0 />
		
		<cfset aggregator = getAggregatorService().getAggregator(arguments.event.getArg("aggregatorId", "")) />
		
		<cfreturn aggregator />
	</cffunction>

	<cffunction name="processAggregatorForm" access="public" returntype="void" output="false"
		hint="Processes the aggregator form and announces the next event.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var aggregator = arguments.event.getArg("aggregator") />
		<cfset var results = StructNew() />

		<cfset results.success = true />
				
		<cfif arguments.event.isArgDefined("isActive")>
			<cfset aggregator.getAudit().setIsActive(arguments.event.getArg("isActive")) />
		</cfif>
		
		<cfif aggregator.getAggregatorId() EQ "">
			<cfset aggregator.getAudit().setCreatedById(getUserFacade().getUser().getUserId()) />
			<cfset aggregator.getAudit().setDtCreated(getProperty("resourceBundleService").getLocaleUtils().toEpoch(Now())) />
			<cfset aggregator.getAudit().setIpCreated(CGI.REMOTE_ADDR) />
			
			<cftry>
				<cfset getAggregatorService().createAggregator(aggregator) />
				<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("aggregatorcreatesuccessmessage") />
				<cfcatch type="any">
					<cfset results.success = false />
					<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("aggregatorcreatefailedmessage") />
				</cfcatch>
			</cftry>
		<cfelse>
			<cfset aggregator.getAudit().setModifiedById(getUserFacade().getUser().getUserId()) />
			<cfset aggregator.getAudit().setDtModified(getProperty("resourceBundleService").getLocaleUtils().toEpoch(Now())) />
			<cfset aggregator.getAudit().setIpModified(CGI.REMOTE_ADDR) />
			
			<cftry>
				<cfset getAggregatorService().updateAggregator(aggregator) />
				<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("aggregatorupdatesuccessmessage") />
				<cfcatch type="any">
					<cfset results.success = false />
					<cfset results.message = getProperty("resourceBundleService").getResourceBundle().getResource("aggregatorupdatefailedmessage") />
				</cfcatch>
			</cftry>
		</cfif>

		<cfset arguments.event.setArg("message", results.message) />
		
		<cfif results.success>
			<cfset announceEvent("pass", arguments.event.getArgs()) />
		<cfelse>
			<cfset announceEvent("fail", arguments.event.getArgs()) />
		</cfif>
	</cffunction>
	
	<cffunction name="deleteAggregator" access="public" returntype="void" output="false"
		hint="Deletes an aggregator and announces the next event.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />
		
		<cfset var aggregator = arguments.event.getArg("aggregator") />
		<cfset var message = getProperty("resourceBundleService").getResourceBundle().getResource("aggregatordeletesuccessmessage") />
		<cfset var success = true />
		
		<cftry>
			<cfset getAggregatorService().deleteAggregator(aggregator) />
			<cfcatch type="any">
				<cfset success = false />
				<cfset message = getProperty("resourceBundleService").getResourceBundle().getResource("aggregatordeletefailedmessage") />
			</cfcatch>
		</cftry>
		
		<cfset arguments.event.setArg("message", message) />
		
		<cfif success>
			<cfset announceEvent("pass", arguments.event.getArgs()) />
		<cfelse>
			<cfset announceEvent("fail", arguments.event.getArgs()) />
		</cfif>
	</cffunction>

</cfcomponent>